#include <modules\graphics\animation.h>

namespace DelGE
{
	namespace Modules
	{
		namespace Graphics
		{
			Animation::Animation()
			{
				this->sName = "";
				this->bPlaying = false;
				this->fFPS = 0;
				this->fFrameTimer = 0;
				this->iStartFrame = this->iEndFrame = this->iCurrentFrame = 0;
				this->ePlaystyle = DelGEGraphics_Animation_Playstyle_None;
				this->iDirection = 1;
			}
			
			Animation::Animation(string sName, int iStartFrame, int iEndFrame, float fFPS)
			{
				this->sName = sName;
				this->bPlaying = false;
				this->fFPS = fFPS;
				this->fFrameTimer = 0;
				this->iStartFrame = iStartFrame;
				this->iCurrentFrame = this->iStartFrame;
				this->iEndFrame = iEndFrame;
				this->ePlaystyle = DelGEGraphics_Animation_Playstyle_None;
				this->iDirection = 1;
			}

			Animation::~Animation()
			{
				this->sName = "";
				this->bPlaying = false;
				this->fFPS = 0;
				this->fFrameTimer = 0;
				this->iStartFrame = this->iEndFrame = this->iCurrentFrame = 0;
				this->ePlaystyle = DelGEGraphics_Animation_Playstyle_None;
				this->iDirection = 0;
			}

			void Animation::SetStartFrame(int iStartFrame)
			{
				this->iStartFrame = iStartFrame;
			}

			void Animation::SetEndFrame(int iEndFrame)
			{
				this->iEndFrame = iEndFrame;
			}

			void Animation::SetFPS(float fFPS)
			{
				this->fFPS = fFPS;
			}

			void Animation::SetPlaystyle(DelGEGraphics_Animation_Playstyle ePlaystyle)
			{
				this->ePlaystyle = ePlaystyle;
			}

			string Animation::GetName()
			{
				return this->sName;
			}

			int Animation::GetStartFrame()
			{
				return this->iStartFrame;
			}

			int Animation::GetEndFrame()
			{
				return this->iEndFrame;
			}

			float Animation::GetFPS()
			{
				return this->fFPS;
			}

			int Animation::GetCurrentFrame()
			{
				return this->iCurrentFrame;
			}

			int Animation::GetNextFrame()
			{
				int iNextFrame = this->iCurrentFrame + iDirection;

				switch (this->ePlaystyle)
				{
					case DelGEGraphics_Animation_Playstyle_None:
						iNextFrame = 0;

					case DelGEGraphics_Animation_Playstyle_Once:
					case DelGEGraphics_Animation_Playstyle_Loop:
						if (iNextFrame > this->iEndFrame)
							iNextFrame = this->iEndFrame;
						break;

					case DelGEGraphics_Animation_Playstyle_Loopback:
						if (iNextFrame > this->iEndFrame)
							iNextFrame = this->iStartFrame;
						else if (iNextFrame < this->iStartFrame)
							iNextFrame = this->iEndFrame;
						break;
				}

				return iNextFrame;
			}

			int Animation::GetFrameCount()
			{
				return this->GetStartFrame() - this->GetEndFrame();
			}

			float Animation::GetCurrentFrameTimer()
			{
				return this->fFrameTimer;
			}

			DelGEGraphics_Animation_Playstyle Animation::GetPlaystyle()
			{
				return this->ePlaystyle;
			}

			bool Animation::IsPlaying()
			{
				return this->bPlaying;
			}

			void Animation::Start()
			{
				this->bPlaying = true;
				this->iCurrentFrame = this->iStartFrame;
				this->iDirection = 1;
			}

			void Animation::Pause()
			{
				this->bPlaying = false;
			}

			void Animation::Resume()
			{
				this->bPlaying = true;
			}

			void Animation::Stop()
			{
				this->bPlaying = false;
				this->iCurrentFrame = this->iStartFrame;
				this->fFrameTimer = 0;
			}

			void Animation::Update(float fDelta)
			{
				if (this->bPlaying)
				{
					this->fFrameTimer += this->fFPS * fDelta;

					if (this->fFrameTimer >= 1)
					{
						this->fFrameTimer = 0;
						this->iCurrentFrame += this->iDirection;

						switch (this->ePlaystyle)
						{
						case DelGEGraphics_Animation_Playstyle_None:
						case DelGEGraphics_Animation_Playstyle_Once:
							if (this->iCurrentFrame > this->iEndFrame)
								this->Stop();
							break;

						case DelGEGraphics_Animation_Playstyle_Loop:
							if (this->iCurrentFrame > this->iEndFrame)
								this->iCurrentFrame = this->iStartFrame;
							break;

						case DelGEGraphics_Animation_Playstyle_Loopback:
							if (this->iCurrentFrame >= this->iEndFrame)
							{
								this->iCurrentFrame = this->iEndFrame;
								this->iDirection *= -1;
								//this->iCurrentFrame += this->iDirection;
							}
							else if (this->iCurrentFrame <= this->iStartFrame)
							{
								this->iCurrentFrame = this->iStartFrame;
								this->iDirection *= -1;
								//this->iCurrentFrame += this->iDirection;
							}
							break;
						}
					}
				}
			}
		}
	}
}